/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.info.client  
 */
package com.apache.client;

import java.util.Map;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.attachment.mommon.ToolsUtil;
import com.apache.rpc.client.RpcProtoclClient;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.StrUtil;

/**
 * description:  信息消息客户端
 * @author dwusoft 创建时间：2015-10-15  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class InfoMQCoreClient {

	private static InfoMQCoreClient instance;
	private static final String serviceName = "infoService";
	private static final String sysKey = "push_to_dwusoft_pass";

	private InfoMQCoreClient() {
	}

	public static InfoMQCoreClient getInstance() {
		if (null == instance) {
			instance = new InfoMQCoreClient();
		}
		return (instance);
	}

	/**
	 * description:  保存信息
	 */
	public ResultMsg saveInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ToolsUtil.getInstance().getValueByKey(sysKey));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "insertInfo", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "保存成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  修改信息
	 */
	public ResultMsg updateInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ToolsUtil.getInstance().getValueByKey(sysKey));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "updateInfo", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "操作成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  删除信息
	 */
	public ResultMsg deleteInfo(Map map) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		map.put("sysPass", ToolsUtil.getInstance().getValueByKey(sysKey));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "deleteInfo", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", "操作成功");
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  查询信息列表
	 */
	public Object selectInfo(Map map) {
		map.put("sysPass", ToolsUtil.getInstance().getValueByKey(sysKey));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "selectInfo", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}

	/**
	 * description:  查询单个信息
	 */
	public Object selectSingle(Map map) {
		map.put("sysPass", ToolsUtil.getInstance().getValueByKey(sysKey));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "selectSingle", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
			return entity.getEntity();
		}
		return null;
	}

	/**
	 * description:  查询单个信息
	 */
	public long countInfo(Map map) {
		map.put("sysPass", ToolsUtil.getInstance().getValueByKey(sysKey));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "countInfo", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			return Long.valueOf(String.valueOf(entity.getEntity()));
		}
		return 0;
	}

	private InterfaceRegister getInterfaceRegister() {
		InterfaceRegister register = new InterfaceRegister();
		String reqUrl = ToolsUtil.getInstance().getValueByKey("info_release_url");
		String infoUrl = reqUrl + "/info/service/doService.action";
		if (reqUrl.startsWith("https:")) {
			register.setCallType("https");
			register.setAddress(infoUrl);
		} else if (reqUrl.startsWith("http:")) {
			register.setCallType("http");
			register.setAddress(infoUrl);
		} else {
			String port = ToolsUtil.getInstance().getValueByKey("info_release_port");
			register.setAddress(reqUrl);
			register.setPort(port);
			register.setCallType("socket");
		}
		return register;
	}
}
